package wh.coach.pivot3.table;

import com.google.common.collect.Lists;
import wh.coach.domain.CalType;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author xhw
 * @date 2024/1/17
 */

public class StudentTable {

    public static PivotTableView generatePivotTableView(Groups groups, List<CalType> calTypes) {
        PivotTableView pivotTableView = new PivotTableView();

        //将groups里的数据拿出来并且排序
        final ArrayList<GroupKey> groupKeys = new ArrayList<>(groups.getKetSet());
        //1.先拿到groups里的rowsKeys去重
        List<List<String>> rowKeys = groupKeys.stream().map(GroupKey::getRowsKeys).distinct().toList();
        //拿到groups里的columnsKeys去重
        List<List<String>> columnKeys = groupKeys.stream().map(GroupKey::getColumnsKeys).distinct().toList();

        int columnSize = 3 + columnKeys.size() * calTypes.size();
        //构建表头
        List<String> header = Lists.newArrayListWithExpectedSize(columnSize);
        header.add("学校");
        header.add("年级");
        header.add("班级");

        //拿到groups里的columnsKeys去重后把数据拼接上去
        for (List<String> columnKey : columnKeys) {
            for (CalType calType : calTypes) {
                header.add(columnKey + calType.getName());
            }
        }
        pivotTableView.setHeader(header);

        // 遍历rowKeys然后加上聚合结果
        for (List<String> rowKey : rowKeys) {
            //拼接一个结果
            List<String> row = Lists.newArrayListWithExpectedSize(columnSize);
            row.addAll(rowKey);

            for (List<String> columnKey : columnKeys) {
                //4.根据新的key去拿到成绩
                GroupKey groupKey = new GroupKey(rowKey, columnKey);
                for (CalType calType : calTypes) {
                    Number value = groups.calculate(calType, groupKey);
                    row.add(Optional.ofNullable(value).orElse(0).toString());
                }
            }

            //添加结果
            pivotTableView.addRow(row);

        }

        return pivotTableView;
    }

    public static Groups groupStudentScores(List<StudentScore> studentScoreList) {
        final Groups groups = new Groups();

        for (StudentScore studentScore : studentScoreList) {
            groups.addStudentScore(studentScore);
        }

        return groups;

    }

    public static List<StudentScore> readStudentScoreList(InputStream inputStream) {
        List<StudentScore> studentScoreList = new ArrayList<>();

        try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
            String line = null;

            //读取文件跳过标签头
            boolean skipHeader = true;
            while ((line = br.readLine()) != null) {

                if (skipHeader) {
                    skipHeader = false;
                    //跳过标签头,下一个循环
                    continue;
                }

                final String[] split = line.split(",");
                if (split.length >= 8) {
                    String school = split[1];
                    String grade = split[2];
                    String className = split[3];
                    String branch = split[4];
                    String subject = split[6];
                    int score = Integer.parseInt(split[7]);

                    studentScoreList.add(new StudentScore(school, grade, className, branch, subject, score));
                }
            }
        } catch (IOException e) {
            //将异常打印到日志中
            e.printStackTrace();
        }
        return studentScoreList;
    }
}
